package com.hjm.mall.config.handler;

import com.hjm.mall.common.Constants;
import com.hjm.mall.common.ServiceResultEnum;
import com.hjm.mall.config.exception.TokenVerifyException;
import com.hjm.mall.config.annotation.TokenToMallUserId;
import com.hjm.mall.dao.MallUserMapper;
import com.hjm.mall.dao.MallUserTokenMapper;
import com.hjm.mall.entity.MallUser;
import com.hjm.mall.entity.MallUserToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

@Component
public class TokenToMallUserIdMethodArgumentResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private MallUserMapper mallUserMapper;
    @Autowired
    private MallUserTokenMapper mallUserTokenMapper;
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        if (methodParameter.hasParameterAnnotation(TokenToMallUserId.class)) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        if(methodParameter.getParameterAnnotation(TokenToMallUserId.class) instanceof TokenToMallUserId){
            String token = nativeWebRequest.getHeader("token");   // 获取请求头的token
            System.out.println("获取到的请求头中的token："+token);

            if (null != token && !"".equals(token) && token.length() == Constants.TOKEN_LENGTH) {
                // 获取到 mallUserToken 对象
                MallUserToken mallUserToken = mallUserTokenMapper.selectByToken(token);
                // 检查token是否过期，过期就返回 "无效认证！请重新登录！" 的信息
                if (mallUserToken == null || mallUserToken.getExpireTime().getTime() <= System.currentTimeMillis()) {
                    TokenVerifyException.fail(ServiceResultEnum.TOKEN_EXPIRE_ERROR.getResult());
                }

                // 通过 mallUserToken 对象的userId获取到 mallUser 对象
                MallUser mallUser = mallUserMapper.selectByPrimaryKey(mallUserToken.getUserId());
                // 如果获取不到 mallUser 对象说明用户被注销或者删除，返回 ”无效用户！请重新登录“ 的信息
                if (mallUser == null) {
                   TokenVerifyException.fail(ServiceResultEnum.USER_NULL_ERROR.getResult());
                }
                // 如果用户被锁定就返回 "用户已被禁止登录！" 的信息
                if (mallUser.getLockedFlag().intValue() == 1) {
                   TokenVerifyException.fail(ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult());
                }
                // 以上情况都没有出现就返回 mallUser 的id
                return mallUser.getUserId();
            }
            TokenVerifyException.fail(ServiceResultEnum.NOT_LOGIN_ERROR.getResult());
        }
        return null;
    }
}
